package com.audi.view;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.audi.R;
import com.audi.activity.AddActionCardActivity;
import com.audi.adapter.AddActionCardAdapter;
import com.audi.dto.SmartTriggerTriggerItem;
import com.audi.dto.SmartTriggerWhenItem;
import com.audi.utils.Constants;

public class DialogSelectActionTrigger {
	Activity activity;
	public SmartTriggerWhenItem mStriggerTwhenItem = new SmartTriggerWhenItem();
	public SmartTriggerTriggerItem mStriggerthenItem = new SmartTriggerTriggerItem();
	public String desWhen = "", desThen = "";
	int deviceType;

	public DialogSelectActionTrigger(Activity activity) {
		this.activity = activity;
	}

	@SuppressLint("DefaultLocale")
	public void selectAction(SmartTriggerWhenItem mSTwhenItem,
			SmartTriggerTriggerItem mSTthenItem) {
		this.mStriggerTwhenItem = mSTwhenItem;
		this.mStriggerthenItem = mSTthenItem;
		if (mSTwhenItem != null) {
			deviceType = mSTwhenItem.getWhenDeviceType();
		} else if (mSTthenItem != null) {
			deviceType = mSTthenItem.getTriggerDeviceType();
		}

		final ArrayList<String> maction = new ArrayList<String>();
		ArrayList<String> mTime = new ArrayList<String>();
		ListView listTrigger;
		final Dialog mDialog = new Dialog(activity);
		mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		mDialog.setContentView(R.layout.dialog_select_action_trigger);
		TextView title = (TextView) mDialog.findViewById(R.id.title_dialog);
		title.setText("Select action");
		listTrigger = (ListView) mDialog.findViewById(R.id.list_action_trigger);

		mDialog.show();
		AddActionCardAdapter smartadap;
		mTime = new ArrayList<String>();
		mTime.add("Every day at");
		mTime.add("Every hour at");
		mTime.add("Every day of the week at");

		switch (deviceType) {
		case 0:
		case 1:
		case 2:
			maction.clear();
			for (int j3 = 0; j3 < Constants.ONOFF.values().length; j3++) {
				if (mSTwhenItem != null) {
					maction.add(mSTwhenItem.getWhenDeviceName() + " "
							+ activity.getString(R.string.when_trigger_sw)
							+ " " + Constants.ONOFF.values()[j3]);
				} else if (mSTthenItem != null) {
					maction.add(activity.getString(R.string.when_trigger_turn)
							+ " "
							+ mSTthenItem.getTriggerDeviceName()
							+ " "
							+ Constants.ONOFF.values()[j3].toString()
									.toLowerCase());
				}
			}
			smartadap = new AddActionCardAdapter(activity, maction);
			break;
		case 3:
			maction.clear();
			for (int j3 = 0; j3 < Constants.ONOFF_DOOR.values().length; j3++) {
				if (mSTwhenItem != null) {
					maction.add(mSTwhenItem.getWhenDeviceName() + " "
							+ Constants.ONOFF_DOOR.values()[j3]);
				} else if (mSTthenItem != null) {
					maction.add(mSTthenItem.getTriggerDeviceName()
							+ " "
							+ Constants.ONOFF_DOOR.values()[j3].toString()
									.toLowerCase());
				}
			}
			smartadap = new AddActionCardAdapter(activity, maction);
			break;
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			maction.clear();
			if (mSTwhenItem != null) {
				maction.add(mSTwhenItem.getWhenDeviceName() + " "
						+ activity.getString(R.string.when_trigger_warning));
			}
			smartadap = new AddActionCardAdapter(activity, maction);
			break;
		case 11:
			maction.clear();
			String[] listwarningtemperature = activity.getResources()
					.getStringArray(R.array.list_warning_temperature);
			for (int j3 = 0; j3 < Constants.TEM_WARNING.values().length; j3++) {
				if (mSTwhenItem != null) {
					maction.add(mSTwhenItem.getWhenDeviceName() + " "
							+ listwarningtemperature[j3]);
				}
			}
			smartadap = new AddActionCardAdapter(activity, maction);
			break;
		case 12:
			maction.clear();

			String[] listwarningtemhum = activity.getResources()
					.getStringArray(R.array.list_warning_humidity);
			for (int j3 = 0; j3 < Constants.HUM_WARNING.values().length; j3++) {
				if (mSTwhenItem != null) {
					maction.add(mSTwhenItem.getWhenDeviceName() + " "
							+ listwarningtemhum[j3]);
				}
			}

			smartadap = new AddActionCardAdapter(activity, maction);
			break;
		case 13:
			maction.clear();
			String[] listwarningdoor = activity.getResources().getStringArray(
					R.array.list_warning_door_window);

			if (mSTwhenItem != null) {
				for (int doorint = 0; doorint < listwarningdoor.length; doorint++) {
					maction.add(mSTwhenItem.getWhenDeviceName() + " "
							+ listwarningdoor[doorint]);
				}
			}
			smartadap = new AddActionCardAdapter(activity, maction);
			break;
		case 14:
			maction.clear();
			String[] listwarningwindow = activity.getResources()
					.getStringArray(R.array.list_warning_door_window);

			if (mSTwhenItem != null) {
				for (int doorint = 0; doorint < listwarningwindow.length; doorint++) {
					maction.add(mSTwhenItem.getWhenDeviceName() + " "
							+ listwarningwindow[doorint]);
				}
			}
			smartadap = new AddActionCardAdapter(activity, maction);
			break;
		case 15:
			maction.clear();
			if (mSTwhenItem != null) {
				maction.add(activity
						.getString(R.string.when_trigger_warning_motion));
			}
			smartadap = new AddActionCardAdapter(activity, maction);
			break;
		case 18:
			maction.clear();
			if (mSTthenItem != null) {
				maction.add(activity.getString(R.string.when_trigger_sence)
						+ " " + mSTthenItem.getTriggerDeviceName());
			}
			smartadap = new AddActionCardAdapter(activity, maction);
			break;

		default:
			// maction.add(Constants.TEM_WARNING.values()[1] + "");
			smartadap = new AddActionCardAdapter(activity, mTime);
			break;
		}
		listTrigger.setAdapter(smartadap);

		listTrigger.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int value,
					long arg3) {
				if (deviceType == 16) {
					switch (value) {
					case 0:
						EditTimeHourDialog diahour = new EditTimeHourDialog(
								activity);
						diahour.dialogEditTimeHour();
						// chonTrigger(null, value);
						// returnVal(action, deviceType, value);
						mDialog.dismiss();
						break;
					case 1:
						EditTimeMinutesDialog time = new EditTimeMinutesDialog(
								activity);
						// chonTrigger(deviceItem, value);
						// returnVal(action, deviceType, value);
						mDialog.dismiss();
						time.dialogEditTimeMinutes();
						break;
					case 2:
						mDialog.dismiss();
						EditDayDialog dayDialog = new EditDayDialog(activity);
						dayDialog.dialogEditDayOfWeek();
						// returnVal(action, deviceType, value);

						break;

					default:
						break;
					}
				} else {
					mDialog.dismiss();
					if (mStriggerTwhenItem != null) {
						mStriggerTwhenItem.setWhenDeviceValues(String
								.valueOf(value));
						desWhen = maction.get(value);
					} else if (mStriggerthenItem != null) {
						mStriggerthenItem.setTriggerDeviceValues(String
								.valueOf(value));
						desThen = maction.get(value);
					}
					((AddActionCardActivity) activity)
							.loadDescriptionHomeAction();
				}

			}
		});
	}

	public void initDesWhenThen(SmartTriggerWhenItem mSTwhenItem,
			SmartTriggerTriggerItem mSTthenItem) {
		switch (deviceType) {
		case 0:
		case 1:
		case 2:
			for (int j3 = 0; j3 < Constants.ONOFF.values().length; j3++) {
				if (mSTwhenItem != null) {
					desWhen = mSTwhenItem.getWhenDeviceName() + " "
							+ activity.getString(R.string.when_trigger_sw)
							+ " " + Constants.ONOFF.values()[j3];

				} else if (mSTthenItem != null) {
					desThen = activity.getString(R.string.when_trigger_turn)
							+ " "
							+ mSTthenItem.getTriggerDeviceName()
							+ " "
							+ Constants.ONOFF.values()[j3].toString()
									.toLowerCase();
				}
				break;
			}
			break;
		case 3:
			for (int j3 = 0; j3 < Constants.ONOFF_DOOR.values().length; j3++) {
				if (mSTwhenItem != null) {
					desWhen = mSTwhenItem.getWhenDeviceName() + " "
							+ Constants.ONOFF_DOOR.values()[j3];
				} else if (mSTthenItem != null) {
					desThen = mSTthenItem.getTriggerDeviceName()
							+ " "
							+ Constants.ONOFF_DOOR.values()[j3].toString()
									.toLowerCase();
				}
				break;
			}
			break;
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			if (mSTwhenItem != null) {
				desWhen = mSTwhenItem.getWhenDeviceName() + " "
						+ activity.getString(R.string.when_trigger_warning);
			}
			break;
		case 11:
			String[] listwarningtemperature = activity.getResources()
					.getStringArray(R.array.list_warning_temperature);
			for (int j3 = 0; j3 < Constants.TEM_WARNING.values().length; j3++) {
				if (mSTwhenItem != null) {
					desWhen = mSTwhenItem.getWhenDeviceName() + " "
							+ listwarningtemperature[j3];
				}
				break;
			}
			break;
		case 12:
			String[] listwarningtemhum = activity.getResources()
					.getStringArray(R.array.list_warning_humidity);
			for (int j3 = 0; j3 < Constants.HUM_WARNING.values().length; j3++) {
				if (mSTwhenItem != null) {
					desWhen = mSTwhenItem.getWhenDeviceName() + " "
							+ listwarningtemhum[j3];
				}
				break;
			}
			break;
		case 13:
			String[] listwarningdoor = activity.getResources().getStringArray(
					R.array.list_warning_door_window);
			if (mSTwhenItem != null) {
				for (int doorint = 0; doorint < listwarningdoor.length; doorint++) {
					desWhen = mSTwhenItem.getWhenDeviceName() + " "
							+ listwarningdoor[doorint];
					break;
				}
			}
			break;
		case 14:
			String[] listwarningwindow = activity.getResources()
					.getStringArray(R.array.list_warning_door_window);
			if (mSTwhenItem != null) {
				for (int doorint = 0; doorint < listwarningwindow.length; doorint++) {
					desWhen = mSTwhenItem.getWhenDeviceName() + " "
							+ listwarningwindow[doorint];
					break;
				}
			}
			break;
		case 15:
			if (mSTwhenItem != null) {
				desWhen = activity
						.getString(R.string.when_trigger_warning_motion);
			}
			break;
		case 18:
			if (mSTthenItem != null) {
				desWhen = activity.getString(R.string.when_trigger_sence) + " "
						+ mSTthenItem.getTriggerDeviceName();
			}
			break;

		default:
			// maction.add(Constants.TEM_WARNING.values()[1] + "");
			// smartadap = new AddActionCardAdapter(activity, mTime);
			break;
		}
	}
}
